using System;
using System.Data;
using System.IO;
using System.Web.UI.WebControls;
using vs.Core;

namespace Forum
{
    public partial class forum_home_setting : WindowSetting
    {
        private void BindSkin()
        {
            string virpath = ResolveUrl("~/desktops/forum/template/");
            string path = Server.MapPath(virpath);
            DirectoryInfo dir = new DirectoryInfo(path);
            DirectoryInfo[] dirs = dir.GetDirectories();

            ddlSkin.Items.Add(new ListItem("", ""));
            for (int i = 0; i < dirs.Length; i++)
            {
                string dirname = ImageLib.GetFileName(dirs[i].FullName, '\\');
                ddlSkin.Items.Add(new ListItem(dirname, dirname));
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    BindSkin();
                    vs.Core.PortalUtils.FillDesktops(ddlHome);
                    vs.Core.PortalUtils.FillDesktops(ddlForum);
                    vs.Core.PortalUtils.FillDesktops(ddlTopic);
                    vs.Core.PortalUtils.FillDesktops(ddlSearch);

                    string homedesk = this.GetModuleSetting("homedesk");
                    if (homedesk != "") ddlHome.SelectedValue = homedesk;

                    string forumdesk = this.GetModuleSetting("forumdesk");
                    if (forumdesk != "") ddlForum.SelectedValue = forumdesk;

                    string topicdesk = this.GetModuleSetting("topicdesk");
                    if (topicdesk != "") ddlTopic.SelectedValue = topicdesk;

                    string searchdesk = this.GetModuleSetting("searchdesk");
                    if (searchdesk != "") ddlSearch.SelectedValue = searchdesk;

                    string skin = this.GetModuleSetting("skin");
                    if (skin != "") ddlSkin.SelectedValue = skin;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.SaveModuleSetting("homedesk", ddlHome.SelectedValue);
                this.SaveModuleSetting("forumdesk", ddlForum.SelectedValue);
                this.SaveModuleSetting("topicdesk", ddlTopic.SelectedValue);
                this.SaveModuleSetting("searchdesk", ddlSearch.SelectedValue);
                this.SaveModuleSetting("skin", ddlSkin.SelectedValue);

                btnCancel_Click(null, new EventArgs());
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            string deskname = PortalUtils.GetDesktopNameByWindowID(this.WindowID);
            Response.Redirect(UrlProvider.DesktopUrl(deskname));
        }
    }
}